﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBBHotelReservation
{
    public partial class frmMain : Form
    {
        UnityElement userData = new UnityElement(new object[] { });
        public frmMain()
        {
            InitializeComponent();

            this.Resize += new EventHandler(frmMain_Resize);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
            this.Load += new EventHandler(frmMain_Load);
            this.Shown += new EventHandler(frmMain_Shown);
            sldMain.ItemRemoved += new EventHandler(sldMain_ItemRemoved);
            sldMain.ItemAdded += new EventHandler(sldMain_ItemAdded);
            sldMain.AnimationCompleted += new EventHandler(sldMain_AnimationCompleted);
            mnuCloseAll.Click += new EventHandler(mnuCloseAll_Click);

            //Menus
            mnuAbout.Click += new EventHandler(mnuAbout_Click);
            mnuExit.Click += new EventHandler(mnuExit_Click);
            mnuSettings.Click += new EventHandler(mnuSettings_Click);
            mnuGuestProfileEntry.Click += new EventHandler(mnuGuestProfileEntry_Click);
            mnuExpenses.Click += new EventHandler(mnuExpenses_Click);
            mnuCheck.Click += new EventHandler(mnuCheck_Click);
            mnuCashDenomination.Click += new EventHandler(mnuCashDenomination_Click);
            mnuLookUpManager.Click += new EventHandler(mnuLookUpManager_Click);
            mnuOtherAmenities.Click += new EventHandler(mnuOtherAmenities_Click);
            mnuHotelRoomManagement.Click += new EventHandler(mnuHotelRoomManagement_Click);
            mnuDailySalesViewer.Click += new EventHandler(mnuDailySalesViewer_Click);
            mnuTransaction.Click += new EventHandler(mnuTransaction_Click);
            mnuFoodManager.Click += new EventHandler(mnuFoodManager_Click);
            mnuAmenitiesPriceManager.Click += new EventHandler(mnuAmenitiesPriceManager_Click);
            mnuPackageCreator.Click += new EventHandler(mnuPackageCreator_Click);
            mnuRateManager.Click += new EventHandler(mnuRateManager_Click);
            //Users
            mnuUserManager.Click += new EventHandler(mnuUserManager_Click);
            mnuLogin.Click += new EventHandler(mnuLogin_Click);
            
        }

        void mnuRateManager_Click(object sender, EventArgs e)
        {
            frmRateManager frm = new frmRateManager();
            frm.Initialize("Room and Function Hall Rate Manager");
            sldMain.AddItem(frm.PanelForm, "Room and Function Hall Rate Manager"); 
        }

        void frmMain_Shown(object sender, EventArgs e)
        {
            DoLogin();
        }
        void mnuLogin_Click(object sender, EventArgs e)
        {
            DoLogin();
        }
        void mnuPackageCreator_Click(object sender, EventArgs e)
        {
            frmPackageCreator frm = new frmPackageCreator();
            frm.Initialize("Package Creator");
            sldMain.AddItem(frm.PanelForm, "Package Creator");   
        }
        void mnuAmenitiesPriceManager_Click(object sender, EventArgs e)
        {
            frmAmenitiesManager frm = new frmAmenitiesManager();
            frm.Initialize("Amenities Price Manager");
            sldMain.AddItem(frm.PanelForm, "Amenities Price Manager");   
        }
        void mnuFoodManager_Click(object sender, EventArgs e)
        {
            frmFoodManager frm = new frmFoodManager();
            frm.Initialize("Food Manager");
            sldMain.AddItem(frm.PanelForm, "Food Manager");   
        }
        void mnuTransaction_Click(object sender, EventArgs e)
        {
            frmTransactionsManager frm = new frmTransactionsManager();
            frm.Initialize("Transaction Manager");
            sldMain.AddItem(frm.PanelForm, "Transaction Manager");   
        }

        void mnuDailySalesViewer_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmDailySalesViewer frm = new frmDailySalesViewer();
            frm.Initialize("MRCR Daily Sales Viewer");
            sldMain.AddItem(frm.PanelForm, "MRCR Daily Sales Viewer");
        }
        void mnuHotelRoomManagement_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmMonitoringManager frm = new frmMonitoringManager(this);
            frm.Initialize("Hotel Room and Function Hall Monitoring");
            sldMain.AddItem(frm.PanelForm, "Hotel Room and Function Hall Monitoring");
        }
        void mnuUserManager_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            if (sldMain.ItemList.Contains("User Manager")) return;
            frmUserManager frm = new frmUserManager();
            frm.Initialize("User Manager");
            sldMain.AddItem(frm.PanelForm, "User Manager");
        }
        void mnuOtherAmenities_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmAmenities frm = new frmAmenities();
            frm.Initialize("Daily Sales for other Amenities");
            sldMain.AddItem(frm.PanelForm, "Daily Sales for other Amenities");
        }
        void mnuLookUpManager_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmLookUpManager frm = new frmLookUpManager();
            frm.Initialize("Lookup Manager");
            sldMain.AddItem(frm.PanelForm, "Lookup Manager");
        }
        void mnuCashDenomination_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmCashDenomination frm = new frmCashDenomination();
            frm.Initialize("Cash Denomination Entry");
            sldMain.AddItem(frm.PanelForm, "Cash Denomination Entry");
        }
        void mnuCheck_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmCheck frm = new frmCheck();
            frm.Initialize("Check Entry and Viewer");
            sldMain.AddItem(frm.PanelForm, "Check Entry and Viewer");
        }
        void mnuExpenses_Click(object sender, EventArgs e)
        {
            // if (!Common.UserLogged) return;
            frmExpenses frm = new frmExpenses();
            frm.Initialize("Expenses Entry");
            sldMain.AddItem(frm.PanelForm, "Expenses Entry");
        }
        void mnuGuestProfileEntry_Click(object sender, EventArgs e)
        {
           // if (!Common.UserLogged) return;
            frmGuestProfile frm = new frmGuestProfile();
            frm.Initialize("Guest Profile");
            sldMain.AddItem(frm.PanelForm, "Guest Profile");
        }
        void mnuSettings_Click(object sender, EventArgs e)
        {
            bool found = false;
            for (int i = 0; i < sldMain.ItemList.Length; i++)
            {
                if (sldMain.ItemList[i].Equals("Settings")) found = true;
            }
            if (!found)
            {
                frmSettings frm = new frmSettings();
                frm.Initialize("Settings");
                sldMain.AddItem(frm.PanelForm, "Settings");
            }
        }
        void mnuAbout_Click(object sender, EventArgs e)
        {
            frmAboutThis frm = new frmAboutThis();
            frm.Initialize("About Malolos Resort Club Royale Reservation Manager");
            sldMain.AddItem(frm.PanelForm, "About Malolos Resort Club Royale Reservation Manager");
        }
        void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (sldMain.ItemCount > 0)
            {
                string msg = string.Format("There are {0} session(s) running. Are you sure to close the application?", sldMain.ItemCount);
                DialogResult dlg = MessageBox.Show(msg, "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (dlg == DialogResult.No) e.Cancel = true;
            }
        }
        void frmMain_Resize(object sender, EventArgs e)
        {
            sldMain.Width = this.Width - 40;
            sldMain.Height = this.Height - 77;
        }
        void sldMain_ItemRemoved(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuWindow.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuWindow.DropDownItems.Add((i + 1) + "-" + buf[i], null, new EventHandler(window_Click));
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            if (sldMain.ItemCount > 0) mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_ItemAdded(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuWindow.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuWindow.DropDownItems.Add((i + 1) + "-" + buf[i], null, new EventHandler(window_Click));
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_AnimationCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < mnuWindow.DropDownItems.Count; i++)
            {
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
        }
        void window_Click(object sender, EventArgs e)
        {
            int index = mnuWindow.DropDownItems.IndexOf((ToolStripItem)sender);
            for (int i = 0; i < mnuWindow.DropDownItems.Count; i++)
            {
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            sldMain.MoveTo(index);
            mnuWindow.DropDownItems[index].ForeColor = Color.RoyalBlue;
        }
        void mnuCloseAll_Click(object sender, EventArgs e)
        {
            if (sldMain.ItemCount > 0)
            {
                sldMain.Controls.Clear();
            }
        }
        void frmMain_Load(object sender, EventArgs e)
        {
            //Load and apply settings
            frmSettings.LoadSettings();
            //Added this error trapping to prevent force close when loading settings from non-existing file.
            try
            {
                if (frmSettings.setPref[0].ToString() == "True") UnityScriptEngine.Dev.Debug = true;
                if (frmSettings.setPref[1].ToString() == "True") this.WindowState = FormWindowState.Maximized;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid values detected when trying to load the settings.\r\n" +
                    "Please configure the application in the Settings menu.", "Load Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                frmSettings frm = new frmSettings();
                frm.Initialize("Settings");
                sldMain.AddItem(frm.PanelForm, "Settings");
 
            }
        }
        //Custom Operations
        void DoLogin()
        {
            if (userData.Data_FieldCount > 1)
            {
                DialogResult res = MessageBox.Show("Logoff " + userData.Data_GetField(1) + "?", "Logoff", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == System.Windows.Forms.DialogResult.Yes)
                {
                    userData = new UnityElement(new object[] { 0 });

                    mnuUserManager.Enabled = false;
                    mnuSettings.Enabled = false;
                    mnuLookUpManager.Enabled = false;

                    mnuAccountingManager.Enabled = false;
                    mnuGuestManager.Enabled = false;
                    mnuGuestProfileEntry.Enabled = false;
                    mnuExpenses.Enabled = false;
                    mnuCheck.Enabled = false;
                    mnuCashDenomination.Enabled = false;
                    mnuOtherAmenities.Enabled = false;
                    mnuHotelRoomManagement.Enabled = false;
                    mnuDailySalesViewer.Enabled = false;
                    mnuTransaction.Enabled = false;
                    mnuFoodManager.Enabled = false;
                    mnuAmenitiesPriceManager.Enabled = false;
                    mnuPackageCreator.Enabled = false;
                    mnuRateManager.Enabled = false;

                    mnuLogin.Text = "Login";
                }
                return;
            }
            frmLogin frm = new frmLogin();
            DialogResult resdlg = frm.ShowDialog();
            if (resdlg == System.Windows.Forms.DialogResult.OK)
            {
                //Top Level
                userData = frm.UserDetail;
                if (int.Parse(userData.Data_GetField(3).ToString()) == 5)
                {
                    mnuUserManager.Enabled = true;
                    mnuSettings.Enabled = true;
                }
                if (int.Parse(userData.Data_GetField(3).ToString()) > 3)
                {
                    mnuLookUpManager.Enabled = true;
                }
                mnuAccountingManager.Enabled = true;
                mnuGuestManager.Enabled = true;
                mnuGuestProfileEntry.Enabled = true;
                mnuExpenses.Enabled = true;
                mnuCheck.Enabled = true;
                mnuCashDenomination.Enabled = true;
                mnuOtherAmenities.Enabled = true;
                mnuHotelRoomManagement.Enabled = true;
                mnuDailySalesViewer.Enabled = true;
                mnuTransaction.Enabled = true;
                mnuFoodManager.Enabled = true;
                mnuAmenitiesPriceManager.Enabled = true;
                mnuPackageCreator.Enabled = true;
                mnuRateManager.Enabled = true;

                mnuLogin.Text = "Logoff " + userData.Data_GetField(1);
            }
        }
        public void loadTransactionManager(string data, bool state)
        {
            frmTransactionsManager frm = new frmTransactionsManager();
            frm.transactionNumber = data;
            frm.isViewing = true;
            frm.Initialize("Transaction Manager");
            sldMain.AddItem(frm.PanelForm, "Transaction Manager");
        }
    }
}
